namespace AltNetUK.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using SimpleWebServices;

    [XmlRoot(ElementName = "credits")]
    public sealed class Credits : IXmlSerializable
    {
        [XmlIgnore]
        public Collection<Contact> Organizers
        {
            get;
            private set;
        }

        [XmlIgnore]
        public Collection<Company> Hosts
        {
            get;
            private set;
        }

        [XmlIgnore]
        public Collection<Company> Sponsors
        {
            get;
            private set;
        }

        [XmlIgnore]
        public Collection<Company> Supporters
        {
            get;
            private set;
        }

        public XmlSchema GetSchema()
        {
            throw new NotSupportedException();
        }

        public void ReadXml(XmlReader reader)
        {
            if (null == reader)
            {
                throw new ArgumentNullException("reader");
            }

            XmlDocument document = new XmlDocument();
            document.LoadXml(reader.ReadOuterXml());

            this.Organizers = new Collection<Contact>();
            foreach (XmlNode node in document.SelectNodes("/credits/organizers/contact"))
            {
                this.Organizers.Add(node.OuterXml.Deserialize<Contact>());
            }

            this.Hosts = new Collection<Company>();
            foreach (XmlNode node in document.SelectNodes("/credits/hosts/company"))
            {
                this.Hosts.Add(node.OuterXml.Deserialize<Company>());
            }

            this.Sponsors = new Collection<Company>();
            foreach (XmlNode node in document.SelectNodes("/credits/sponsors/company"))
            {
                this.Sponsors.Add(node.OuterXml.Deserialize<Company>());
            }

            this.Supporters = new Collection<Company>();
            foreach (XmlNode node in document.SelectNodes("/credits/supporters/company"))
            {
                this.Supporters.Add(node.OuterXml.Deserialize<Company>());
            }
        }

        public void WriteXml(XmlWriter writer)
        {
        }
    }
}